clear
discard
set more off

adopath ++ "./ado/"

******************
*** Parameters ***
******************

local file_name_consensus "./input/consensus_dataset.csv"  // Data file
local file_name_individual "./input/individual_dataset.csv" // Data file
local output_folder "./output/CG_regressions" // Output directory
local K = 4 // Number of lags to include when calculating local projections
local L = 16 // Number of IRF coefficients to estimate
local alpha = 0.05 // Significance level
local L_NW = 5 // Number of lags for calculating Newey-West standard errors

*****************************
*** Import CONSENSUS data ***
*****************************

import delimited using `file_name_consensus', case(preserve)
rename DATE Date

gen qtr = quarterly(Date,"YQ")
format qtr %tq
tsset qtr // Set to time series

**************************
*** Generate variables ***
**************************

gen Efor_Step2 = Realiz1 - SPFfor_Step2
gen Rfor_Step2 = SPFfor_Step2 - SPFfor_Step3

*******************
*** Regressions ***
*******************

* Clean up
shell rm -r `output_folder'
shell mkdir -p ./output
shell mkdir `output_folder'

* Regressions
newey Efor_Step2 Rfor_Step2, lag(`K')
matrix CG_coefs_cons = e(b)
matrix CG_cov_cons = e(V)

* Save output
mat2txt, matrix(CG_coefs_cons) saving(`output_folder'/CG_coefs_consensus.txt)
mat2txt, matrix(CG_cov_cons) saving(`output_folder'/CG_cov_consensus.txt)

clear

******************************
*** Import INDIVIDUAL data ***
******************************

import delimited using `file_name_individual', case(preserve)

gen qtr = quarterly(DATE,"YQ")
format qtr %tq
xtset ID qtr // Set to panel data

**************************
*** Generate variables ***
**************************

gen Efor_Step2 = Realiz1 - SPFfor_Step2
gen Rfor_Step2 = SPFfor_Step2 - SPFfor_Step3

*******************
*** Regressions ***
*******************

reghdfe Efor_Step2 Rfor_Step2, noabsorb vce(cluster ID DATE)
matrix CG_coefs_micro_no_FE = e(b)
matrix CG_cov_micro_no_FE = e(V)

reghdfe Efor_Step2 Rfor_Step2, absorb(ID) vce(cluster ID DATE)
matrix CG_coefs_micro_FE = e(b)
matrix CG_cov_micro_FE = e(V)

reghdfe Efor_Step2 Rfor_Step2, absorb(DATE) vce(cluster ID DATE)
matrix CG_coefs_idio_no_FE = e(b)
matrix CG_cov_idio_no_FE = e(V)

reghdfe Efor_Step2 Rfor_Step2, absorb(DATE ID) vce(cluster ID DATE)
matrix CG_coefs_idio_FE = e(b)
matrix CG_cov_idio_FE = e(V)

* Save output
mat2txt, matrix(CG_coefs_micro_no_FE) saving(`output_folder'/CG_coefs_micro_no_FE.txt)
mat2txt, matrix(CG_cov_micro_no_FE) saving(`output_folder'/CG_cov_micro_no_FE.txt)

mat2txt, matrix(CG_coefs_micro_FE) saving(`output_folder'/CG_coefs_micro_FE.txt)
mat2txt, matrix(CG_cov_micro_FE) saving(`output_folder'/CG_cov_micro_FE.txt)

mat2txt, matrix(CG_coefs_idio_no_FE) saving(`output_folder'/CG_coefs_idio_no_FE.txt)
mat2txt, matrix(CG_cov_idio_no_FE) saving(`output_folder'/CG_cov_idio_no_FE.txt)

mat2txt, matrix(CG_coefs_idio_FE) saving(`output_folder'/CG_coefs_idio_FE.txt)
mat2txt, matrix(CG_cov_idio_FE) saving(`output_folder'/CG_cov_idio_FE.txt)

